package com.ruoyi.statistics.service.impl;

import com.ruoyi.statistics.domain.OrderChange;
import com.ruoyi.statistics.mapper.SysStatisticsOrderMapper;
import com.ruoyi.statistics.service.ISysStatisticsOrderService;
import com.ruoyi.statistics.service.ISysStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SysStatisticsOrderServiceImpl implements ISysStatisticsOrderService {
    @Autowired
    private SysStatisticsOrderMapper sysStatisticsOrderMapper;

    @Override
    public List<OrderChange> getAllStatisticsOrder(Date startTime, Date endTime) {
        List<OrderChange> result = new ArrayList<>();
        Date currentDate = new Date(startTime.getTime());
        while (!currentDate.after(endTime)) {
            // 查询当前日期的订单数量
            int orderCount = sysStatisticsOrderMapper.getOrderCountByDate(currentDate);

            // 构造OrderChange对象
            OrderChange orderChange = new OrderChange();
            orderChange.setOrderTime(currentDate);
            orderChange.setOrderNum(orderCount);

            result.add(orderChange);

            // 日期加1天
            currentDate = new Date(currentDate.getTime() + TimeUnit.DAYS.toMillis(1));
        }
        return result;
    }

    @Override
    public List<OrderChange> getStoreStatisticsOrder(Date startTime, Date endTime, Long deptId) {
        List<OrderChange> result = new ArrayList<>();
        Date currentDate = new Date(startTime.getTime());
        while (!currentDate.after(endTime)) {
            // 查询当前日期的订单数量
            int orderCount = sysStatisticsOrderMapper.getStoreOrderCountByDate(currentDate,deptId);

            // 构造OrderChange对象
            OrderChange orderChange = new OrderChange();
            orderChange.setOrderTime(currentDate);
            orderChange.setOrderNum(orderCount);

            result.add(orderChange);

            // 日期加1天
            currentDate = new Date(currentDate.getTime() + TimeUnit.DAYS.toMillis(1));
        }
        return result;
    }
}
